package com.carefree.bookmall.controller;

import com.carefree.bookmall.entity.BookType;
import com.carefree.bookmall.service.BookTypeService;
import com.carefree.bookmall.service.OrderService;
import com.carefree.bookmall.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName:PageController
 * @Description:
 * @Author Tengbin
 */
@Controller
@RequestMapping("/bookmall")
public class PageController {


    @Resource
    private UserService userService;

    @Resource
    private OrderService orderService;


    @Resource
    private BookTypeService bookTypeService;


    /**
     * 登录
     *
     * @return login页面
     */
    @GetMapping("/login")
    public String userLoginPage() {
        return "login";
    }

    /**
     * 注册页面
     *
     * @return register页面
     */
    @GetMapping("/register")
    public String registerUserPage() {
        return "register";
    }

    /**
     * 个人中心
     *
     * @return
     */
    @GetMapping("/center")
    public String userCenterPage() {
        return "user_center";
    }

    /**
     * 购物车页面
     *
     * @return
     */
    @GetMapping("/cart")
    public String cartPage() {
        return "cart";
    }


    /**
     * 后台管理页面
     *
     * @param request
     * @return
     */
    @GetMapping("/admin/index")
    public String backendIndexPage(HttpServletRequest request) {
        return "admin/index";
    }

    @GetMapping("/admin/main")
    public String backendMainPage(HttpServletRequest request) {
        // 今日收入
        request.setAttribute("incoming", orderService.queryTodayIncoming());
        // 新增用户
        request.setAttribute("userCount", userService.queryTodayUserCount());
        // 用户总数
        request.setAttribute("usersCounts", userService.queryUserCounts());
        // 下单总量
        request.setAttribute("orderCounts", orderService.queryOrderCounts());

        return "admin/main";
    }

    /**
     * 后台 修改密码页面
     *
     * @return
     */
    @GetMapping("/admin/reset")
    public String backendPasswdPage() {
        return "admin/reset_password";
    }


    /**
     * 添加用户
     *
     * @return
     */
    @GetMapping("/admin/user/insert")
    public String backendInsertUserPage() {
        return "admin/user_add";
    }

    /**
     * 添加商品（图书信息）
     *
     * @return
     */
    @GetMapping("/admin/book/insert")
    public String backendInsertBookPage(HttpServletRequest request) {

        List<BookType> bookTypes = bookTypeService.queryBookTypes();

        request.setAttribute("bookTypes", bookTypes);

        return "admin/book_add";
    }

}
